package com.fearless.crm.workbench.web.controller;

import com.fearless.crm.settings.entity.User;
import com.fearless.crm.settings.service.UserService;
import com.fearless.crm.utils.DateTimeUtil;
import com.fearless.crm.utils.UUIDUtil;
import com.fearless.crm.vo.ContactPaginationVo;
import com.fearless.crm.workbench.entity.Contacts;
import com.fearless.crm.workbench.entity.ContactsRemark;
import com.fearless.crm.workbench.service.ContactsRemarkService;
import com.fearless.crm.workbench.service.ContactsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("contact")
public class ContactController {



    @Autowired(required = false)
    @Qualifier(value = "userServiceImpl")
    private UserService userService;



    @Autowired(required = false)
    @Qualifier(value = "contactsRemarkServiceImpl")
    private ContactsRemarkService contactsRemarkService;

    @Resource
    private ContactsService contactsService;


    @RequestMapping(value = "/getContactList.do",method = RequestMethod.GET)
    @ResponseBody
    public ContactPaginationVo getContactListController(Contacts contacts, Integer pageNo, Integer pageSize){
        System.out.println("进入到getContactListController处理器方法");
        System.out.println("执行联系人的展现操作");

        //计算略过的记录条数
        int skipCount=(pageNo-1)*pageSize;

        Map map=new HashMap();
        map.put("owner",contacts.getOwner());
        map.put("fullname",contacts.getFullname());
        map.put("customerName",contacts.getCustomerId());
        map.put("source",contacts.getSource());
        map.put("birth",contacts.getBirth());
        map.put("skipCount",skipCount);
        map.put("pageSize",pageSize);

        ContactPaginationVo contactPaginationVo=contactsService.getContactListService(map);


        return contactPaginationVo;
    }


//铺垫新增联系人的模态窗口
    @GetMapping("/addContact.do")
    @ResponseBody
    public Map addContactController(){
        System.out.println("铺垫新增联系人的模态窗口");

       List<User>  userList=userService.getUserListService();
        Map map=new HashMap();
        map.put("userList",userList);

        return map;
    }

//执行新增联系人的操作
    @PostMapping("/saveContact.do")
    @ResponseBody
    public Map saveContactController(Contacts contacts, @RequestParam("customerName")String customerName,
                                     HttpSession session){
        System.out.println("进入到saveContactController处理器方法");
        System.out.println("执行新增联系人的操作");
        System.out.println("收到的参数是--->"+contacts.toString());
        String createTime= DateTimeUtil.getSysTime();
        String  createBy = ((User) session.getAttribute("user")).getName();
        String id= UUIDUtil.getUUID();
        contacts.setCreateBy(createBy);
        contacts.setCreateTime(createTime);
        contacts.setId(id);

        Map map=new HashMap();
        map.put("contact",contacts);
        map.put("customerName",customerName);

        boolean flag=contactsService.saveContact(map);

        Map map1=new HashMap();
        map1.put("success",flag);

        return map1;
    }


//铺垫修改联系人的模态窗口
    @PostMapping("/editContact.do")
    @ResponseBody
    public Map<String,Object> editContactController(@RequestParam("contactId")String id){
        System.out.println("进入到editContactController处理器方法");
        System.out.println("铺垫修改联系人的模态窗口");

        List<User> userList = userService.getUserListService();
        Map contactAndCustomer=contactsService.editContact(id);
        Map map=new HashMap();
        map.put("userList",userList);
        map.put("contact",contactAndCustomer.get("contacts"));
        map.put("customer",contactAndCustomer.get("customer"));
        return map;
    }

//执行修改联系人的操作
    @PostMapping("/updateContact.do")
    @ResponseBody
    public Map<String,Object> updateContactController(Contacts contacts,HttpSession session){
        System.out.println("进入到updateContactController处理器方法");
        System.out.println("执行修改联系人的操作");
        String editBy=((User)session.getAttribute("user")).getName();
        String editTime=DateTimeUtil.getSysTime();
        contacts.setEditBy(editBy);
        contacts.setEditTime(editTime);


        boolean flag=contactsService.updateContact(contacts);

        Map map=new HashMap();
        map.put("success",flag);

        return map;
    }

//执行删除联系人的操作
    @PostMapping("/deleteContact.do")
    @ResponseBody
    public Map<String,Object> deleteContactController(@RequestParam("id")String[] ids){
        System.out.println("进入到deleteContactController处理器方法");
        System.out.println("执行联系人的删除操作");


        boolean flag=contactsService.deleteContact(ids);

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("success",flag);

        return hashMap;
    }


//联系人详细页
    @RequestMapping("/detail.do")
    public String contactDetailController(@RequestParam("id") String id, Model model){
        System.out.println("进入到contactDetailController处理器方法");

       Contacts contacts= contactsService.contactDetail(id);

        model.addAttribute("contact",contacts);

        return "forward:/workbench/contacts/detail.jsp";
    }


//展现联系人备注
    @GetMapping("/showContactRemark.do")

    public Map<String,Object> showContactRemark(@RequestParam("id")String id){
        System.out.println("进入到showContactRemark处理器方法");
        System.out.println("执行展现联系人备注的操作");

        List<ContactsRemark> remarks=contactsRemarkService.queryContactRemark(id);






        return  null;
    }


}
